<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>字符串混淆器</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
            background: linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 100%);
            color: #ffffff;
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
            background: rgba(45, 45, 45, 0.8);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
        }

        h1 {
            font-size: 2.5rem;
            font-weight: 700;
            background: linear-gradient(45deg, #00d4ff, #ff6b6b);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            margin-bottom: 10px;
        }

        .subtitle {
            color: #b0b0b0;
            font-size: 1.1rem;
        }

        .input-section {
            margin-bottom: 30px;
        }

        label {
            display: block;
            margin-bottom: 10px;
            font-weight: 600;
            color: #e0e0e0;
        }

        textarea {
            width: 100%;
            min-height: 120px;
            padding: 15px;
            border: 2px solid #444;
            border-radius: 12px;
            background: #2a2a2a;
            color: #ffffff;
            font-size: 14px;
            resize: vertical;
            transition: all 0.3s ease;
        }

        textarea:focus {
            outline: none;
            border-color: #00d4ff;
            box-shadow: 0 0 20px rgba(0, 212, 255, 0.2);
        }

        .controls {
            display: grid;
            grid-template-columns: 1fr 1fr 1fr;
            gap: 20px;
            margin-bottom: 30px;
        }

        .control-group {
            background: #333;
            padding: 20px;
            border-radius: 12px;
            border: 1px solid #444;
        }

        .control-label {
            display: block;
            margin-bottom: 10px;
            font-weight: 600;
            color: #e0e0e0;
            font-size: 0.9rem;
        }

        .number-input {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .number-input button {
            width: 35px;
            height: 35px;
            border: none;
            background: #444;
            color: #fff;
            border-radius: 8px;
            cursor: pointer;
            font-size: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: all 0.2s ease;
        }

        .number-input button:hover {
            background: #555;
            transform: scale(1.1);
        }

        .number-input input {
            width: 60px;
            text-align: center;
            padding: 8px;
            border: 1px solid #444;
            border-radius: 8px;
            background: #2a2a2a;
            color: #fff;
            font-size: 14px;
        }

        .toggle {
            position: relative;
            width: 60px;
            height: 30px;
        }

        .toggle input {
            opacity: 0;
            width: 0;
            height: 0;
        }

        .slider {
            position: absolute;
            cursor: pointer;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: #444;
            transition: .3s;
            border-radius: 30px;
        }

        .slider:before {
            position: absolute;
            content: "";
            height: 22px;
            width: 22px;
            left: 4px;
            bottom: 4px;
            background-color: white;
            transition: .3s;
            border-radius: 50%;
        }

        input:checked + .slider {
            background: linear-gradient(45deg, #00d4ff, #ff6b6b);
        }

        input:checked + .slider:before {
            transform: translateX(30px);
        }

        .action-button {
            width: 100%;
            padding: 15px 30px;
            background: linear-gradient(45deg, #00d4ff, #ff6b6b);
            border: none;
            border-radius: 12px;
            color: white;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            margin-bottom: 20px;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
        }

        .action-button::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
            transition: left 0.5s;
        }

        .action-button:hover::before {
            left: 100%;
        }

        .action-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(0, 212, 255, 0.3);
        }

        .output-section {
            background: #333;
            padding: 20px;
            border-radius: 12px;
            border: 1px solid #444;
        }

        .output-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
        }

        .copy-button {
            background: #00d4ff;
            border: none;
            padding: 8px 16px;
            border-radius: 8px;
            color: white;
            cursor: pointer;
            font-size: 14px;
            transition: all 0.2s ease;
        }

        .copy-button:hover {
            background: #00b8e6;
            transform: scale(1.05);
        }

        .output-text {
            background: #2a2a2a;
            padding: 15px;
            border-radius: 8px;
            border: 1px solid #444;
            font-family: 'Courier New', monospace;
            font-size: 14px;
            line-height: 1.5;
            word-break: break-all;
            min-height: 100px;
        }

        @media (max-width: 768px) {
            .controls {
                grid-template-columns: 1fr;
            }
            
            h1 {
                font-size: 2rem;
            }
            
            .container {
                padding: 20px;
            }
        }

        .tooltip {
            position: relative;
            display: inline-block;
        }

        .tooltip .tooltiptext {
            visibility: hidden;
            width: 200px;
            background-color: #555;
            color: #fff;
            text-align: center;
            border-radius: 6px;
            padding: 8px;
            position: absolute;
            z-index: 1;
            bottom: 125%;
            left: 50%;
            margin-left: -100px;
            opacity: 0;
            transition: opacity 0.3s;
            font-size: 12px;
        }

        .tooltip:hover .tooltiptext {
            visibility: visible;
            opacity: 1;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>字符串混淆器</h1>
            <p class="subtitle">混淆字符串（如秘密、IBAN或令牌），使其可共享和可识别，而不泄露其内容。</p>
        </div>

        <div class="input-section">
            <label for="inputText">要混淆的字符串:</label>
            <textarea id="inputText" placeholder="输入要混淆的文本...">Lorem ipsum dolor sit amet</textarea>
        </div>

        <div class="controls">
            <div class="control-group">
                <span class="control-label tooltip">
                    保留前几位:
                    <span class="tooltiptext">保留字符串开头的字符数量</span>
                </span>
                <div class="number-input">
                    <button onclick="decreaseValue('keepFirst')">-</button>
                    <input type="number" id="keepFirst" value="4" min="0" max="50">
                    <button onclick="increaseValue('keepFirst')">+</button>
                </div>
            </div>

            <div class="control-group">
                <span class="control-label tooltip">
                    保留后几位:
                    <span class="tooltiptext">保留字符串末尾的字符数量</span>
                </span>
                <div class="number-input">
                    <button onclick="decreaseValue('keepLast')">-</button>
                    <input type="number" id="keepLast" value="4" min="0" max="50">
                    <button onclick="increaseValue('keepLast')">+</button>
                </div>
            </div>

            <div class="control-group">
                <span class="control-label tooltip">
                    保留空格:
                    <span class="tooltiptext">是否在混淆时保留空格字符</span>
                </span>
                <label class="toggle">
                    <input type="checkbox" id="keepSpaces" checked>
                    <span class="slider"></span>
                </label>
            </div>
        </div>

        <button class="action-button" onclick="obfuscateText()">混淆字符串</button>

        <div class="output-section">
            <div class="output-header">
                <label>混淆结果:</label>
                <button class="copy-button" onclick="copyResult()">复制</button>
            </div>
            <div class="output-text" id="outputText">Lore★ ★★★★★ ★★★★★ ★★★ amet</div>
        </div>
    </div>

    <script>
        function increaseValue(id) {
            const input = document.getElementById(id);
            const currentValue = parseInt(input.value) || 0;
            input.value = Math.min(currentValue + 1, 50);
            obfuscateText();
        }

        function decreaseValue(id) {
            const input = document.getElementById(id);
            const currentValue = parseInt(input.value) || 0;
            input.value = Math.max(currentValue - 1, 0);
            obfuscateText();
        }

        function obfuscateText() {
            const inputText = document.getElementById('inputText').value;
            const keepFirst = parseInt(document.getElementById('keepFirst').value) || 0;
            const keepLast = parseInt(document.getElementById('keepLast').value) || 0;
            const keepSpaces = document.getElementById('keepSpaces').checked;

            if (!inputText) {
                document.getElementById('outputText').textContent = '';
                return;
            }

            let result = '';
            const textLength = inputText.length;

            // 如果保留的字符数超过文本长度，直接返回原文本
            if (keepFirst + keepLast >= textLength) {
                result = inputText;
            } else {
                // 前部分
                const firstPart = inputText.substring(0, keepFirst);
                
                // 中间部分
                const middlePart = inputText.substring(keepFirst, textLength - keepLast);
                let obfuscatedMiddle = '';
                
                for (let char of middlePart) {
                    if (keepSpaces && char === ' ') {
                        obfuscatedMiddle += ' ';
                    } else if (char === ' ') {
                        obfuscatedMiddle += '★';
                    } else {
                        obfuscatedMiddle += '★';
                    }
                }

                // 后部分
                const lastPart = inputText.substring(textLength - keepLast);

                result = firstPart + obfuscatedMiddle + lastPart;
            }

            document.getElementById('outputText').textContent = result;
        }

        function copyResult() {
            const outputText = document.getElementById('outputText').textContent;
            
            if (!outputText) {
                alert('没有可复制的内容！');
                return;
            }

            navigator.clipboard.writeText(outputText).then(() => {
                const button = document.querySelector('.copy-button');
                const originalText = button.textContent;
                button.textContent = '已复制！';
                button.style.background = '#4CAF50';
                
                setTimeout(() => {
                    button.textContent = originalText;
                    button.style.background = '#00d4ff';
                }, 1000);
            }).catch(() => {
                // 降级方案
                const textArea = document.createElement('textarea');
                textArea.value = outputText;
                document.body.appendChild(textArea);
                textArea.select();
                try {
                    document.execCommand('copy');
                    alert('已复制到剪贴板！');
                } catch (err) {
                    alert('复制失败，请手动复制');
                }
                document.body.removeChild(textArea);
            });
        }

        // 监听输入变化
        document.getElementById('inputText').addEventListener('input', obfuscateText);
        document.getElementById('keepFirst').addEventListener('change', obfuscateText);
        document.getElementById('keepLast').addEventListener('change', obfuscateText);
        document.getElementById('keepSpaces').addEventListener('change', obfuscateText);

        // 初始化
        obfuscateText();

        // 添加键盘快捷键
        document.addEventListener('keydown', (e) => {
            if (e.ctrlKey && e.key === 'Enter') {
                obfuscateText();
            }
            if (e.ctrlKey && e.key === 'c' && e.target.id === 'outputText') {
                copyResult();
            }
        });
    </script>
</body>
</html>